package org.fglxy.mockserver.controller;

import java.io.IOException;

import org.fglxy.mockserver.dto.AllMockResponse;
import org.fglxy.mockserver.dto.AllMockTaskResponse;
import org.fglxy.mockserver.dto.BaseRespDTO;
import org.fglxy.mockserver.dto.CreateMockRequest;
import org.fglxy.mockserver.dto.CreateMockTaskRequest;
import org.fglxy.mockserver.dto.DeleteMockRequest;
import org.fglxy.mockserver.dto.DeleteTaskRequest;
import org.fglxy.mockserver.entity.MockEndEntity;
import org.fglxy.mockserver.enums.RetCodeEnum;
import org.fglxy.mockserver.exception.MessageException;
import org.fglxy.mockserver.service.MockService;
import org.fglxy.mockserver.util.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("api/mock")
public class MockController {
	private static Logger LOGGER = LoggerFactory.getLogger(MockController.class);
	
	
	@Autowired
	MockService service;

	/**
	 * 创建与服务器的mock任务
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public BaseRespDTO createMock(@RequestBody CreateMockRequest request,
			@RequestParam(value = "descript", required = false) MultipartFile descript,
			@RequestParam(value = "cmdNos", required = false) MultipartFile cmdNos) {
		MockEndEntity entity = new MockEndEntity();
		BeanUtils.copyProperties(request, entity);
		if (entity.getId() == null) {
			service.createMock(entity, descript, cmdNos);
		} else {
			service.modifyMock(entity, descript, cmdNos);
		}
		return null;
	}

	/**
	 * 删除mock任务
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public void deleteMock(@RequestBody DeleteMockRequest request) {
		service.deleteMock(request);
	}

	/**
	 * 创建一个mock请求（mockserver模拟的客户端向待测服务器请求）
	 */
	@RequestMapping(value = "createTask", method = RequestMethod.POST)
	public void createTask(@RequestBody CreateMockTaskRequest request) {
		service.createTask(request);
	}

	/**
	 * 删除mock请求
	 */
	@RequestMapping(value = "deleteTask", method = RequestMethod.POST)
	public void deleteTask(@RequestBody DeleteTaskRequest request) {
		service.deleteTask(request);
	}

	/**
	 * 开始执行mock任务
	 */
	@RequestMapping(value = "runMock", method = RequestMethod.GET)
	public BaseRespDTO runMock(String id) {
		LOGGER.info("运行mock计划:{}", id);
		service.runMock(id);
		return new BaseRespDTO();
	}
	
	@RequestMapping(value = "getAllMocks", method = RequestMethod.GET)
	public AllMockResponse getAllMocks() {
		try {
			return new AllMockResponse(ResourceUtils.allMock());
		} catch (IOException e) {
			throw new MessageException(RetCodeEnum.ERROR, e);
		}
	}
	
	@RequestMapping(value = "getAllTasks", method = RequestMethod.GET)
	public AllMockTaskResponse getAllTasks(String mockId) {
		ResourceUtils.readLock(mockId);
		try {
			return new AllMockTaskResponse(ResourceUtils.getTasksByMockId(mockId));
		} catch (IOException e) {
			throw new MessageException(RetCodeEnum.ERROR, e);
		} finally {
			ResourceUtils.releaseReadLock(mockId);
		}
	}
}
